package com.lemon.ai.socket.packets.spiritgame.xls2Object.generator;

import java.io.File;
import java.io.FileInputStream;

import com.lemon.ai.socket.packets.spiritgame.model.generator.AbstractGenerator;
import com.lemon.ai.socket.packets.spiritgame.model.utils.FileUtils;
import com.lemon.ai.socket.packets.spiritgame.xls2Object.Constent;

public class ASClassGenerator extends AbstractGenerator {

	private static FileUtils fu = new FileUtils();

	public ASClassGenerator() {
		super("asClassTemp.txt");

		try {
			this.prop.load(new FileInputStream(new File(System
					.getProperty("user.dir")
					+ "\\templetConfig.properties")));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void generate(Constent c) {
		if(!c.isClientNeed()) {
			return;
		}
		String temp = "//" + c.explain + "\r" ;

		String h = "Vo";

		temp += super.temp.
				replace("PACKAGE_NAME", super.prop.getProperty("package")).
				replace("CLASS_NAME", c.className + h).
				replace("PROPERTIES", buildProperties(c)).
				replace("SETS_AND_GETS", buildGetterSetters(c));

		String fileName = super.prop.getProperty("dir")
				+ super.prop.getProperty("package").replace(".", "\\")
				+ "\\as\\vo\\" + c.className + h
				+ ".as";

		fu.write(fileName, temp);
	}

	private String METHOD_TEMP = 
					"		/**\r" +
					"		 * EXPlAIN\r" +
					"		 */\r" +
					"		public function set METHOD_NAME(PROPERTY_NAME:TYPE_NAME):void {\r" + 
					"			this._PROPERTY_NAME = PROPERTY_NAME;\r" + 
					"		}\r\r" + 
					"		/**\r" +
					"		 * EXPlAIN\r" +
					"		 */\r" +
					"		public function get METHOD_NAME():TYPE_NAME {\r" + 
					"			return this._PROPERTY_NAME;\r" + 
					"		}\r\r";

	private String buildGetterSetters(Constent c) {
		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < c.filedNames.length; i++) {
			if(c.marks[i].equals("1")) {//不为1才生成
				continue;
			}
			String f = c.filedNames[i];
			String t = c.types[i];
			t = parse(t);
			sb.append(
					METHOD_TEMP.
					replaceAll("EXPlAIN", c.explains[i]).
					replaceAll("METHOD_NAME", f).
					replaceAll("TYPE_NAME", t).
					replaceAll("PROPERTY_NAME", f)
					);
		}

		return sb.toString();
	}

	public static String parse(String t) {
		if(t.equals("byte")) 
			return "int";

		if(t.equals("short")) 
			return "int";

		if(t.equals("long")) 
			return "Number";

		if(t.equals("char")) 
			return "int";

		if(t.equals("float")) 
			return "Number";

		if(t.equals("double")) 
			return "Number";

		if(t.equals("boolean")) {
			t = "Boolean";
		}
		return t;
	}

	private String PROPERTY_TEMP = 
					"		/**\r" +
					"		 * EXPlAIN\r" +
					"		 */\r" +
					"		private var _PROPERTY_NAME:TYPE_NAME;\r";

	private String buildProperties(Constent c) {
		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < c.filedNames.length; i++) {
			if(c.marks[i].equals("1")) {//不为1才生成
				continue;
			}
			String f = c.filedNames[i];
			String t = c.types[i];
			t = parse(t);
			sb.append(
					PROPERTY_TEMP.
					replaceAll("EXPlAIN", c.explains[i]).
					replaceAll("TYPE_NAME", t).
					replaceAll("PROPERTY_NAME", f)
					);
		}

		return sb.toString();
	}

}
